﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UnixTimeLocalTime.Common;
using TVPrize.Common;

namespace UnixTimeLocalTime
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //LoginBaidu();
            InitDateTime();
        }

        public void LoginBaidu()
        {
            string loginUrl = "https://passport.baidu.com/?login";
            string userName = "水晶文鑫";
            string password = "";
            string tagUrl = "http://cang.baidu.com/" + userName + "/tags";
            Encoding encoding = Encoding.GetEncoding("gb2312");

            IDictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("tpl", "fa");
            parameters.Add("tpl_reg", "fa");
            parameters.Add("u", tagUrl);
            parameters.Add("psp_tt", "0");
            parameters.Add("username", userName);
            parameters.Add("password", password);
            parameters.Add("mem_pass", "1");
            HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, encoding, null);
            string cookieString = response.Headers["Set-Cookie"]; 
       
            CookieCollection cookies = new CookieCollection();//如何从response.Headers["Set-Cookie"];中获取并设置CookieCollection的代码略  
            response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, cookies);
        }

        private void InitDateTime()
        {
            txtLocalTime.Text = DateTime.Now.ToString();
            txtUnixTime.Text = TimeParser.ConvertDateTimeDouble(DateTime.Now).ToString();
        }

        private void btnUnixTime_Click(object sender, EventArgs e)
        {
            showTimeTxt.Text = TimeParser.ConvertDateTimeDouble(Convert.ToDateTime(txtUnixTime.Text.Trim())).ToString();
        }

        private void btnLocalTime_Click(object sender, EventArgs e)
        {
            showTimeTxt.Text = TimeParser.ConvertDoubleDatetime(Convert.ToDouble(txtLocalTime.Text.Trim())).ToString();
        }

        private void buttonRandom_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(textBoxMin.Text.Trim());
            double b = Convert.ToDouble(textBoxMax.Text.Trim());
            var list=RandomDouble.GenerateRandomOrderedNumbers(a, b, 2);
            foreach (var item in list)
            {
                textBoxValue.Text += (item.ToString() + ",");
            }
        }

        private void btnMd5_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSource.Text))
            {
                MessageBox.Show("请输入加密字符串");
                txtSource.Focus();
            }
            txtDestination.Text = MD5.GetMd5(txtSource.Text);
        }
    }
}
